<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/27
 * Time: 10:19
 */

namespace Wechat\Controller;


class PaymentController extends CommonController {

    /**
     * 留言
     */
    public function index() {
        $customerId = $this->customer["customer_id"];

        $paymentMap["customer_id"] = $customerId;
        $paymentMap["day"] = date("Y-m-d");
        $payment = D("Payment")->queryByMap($paymentMap);

        $this->assign("type",$_REQUEST["type"]);
        $this->assign("customerId", $customerId);
        $this->assign("info", $payment);
        $this->display("index");

    }

    public function createPayment() {

        $data["code"] = 200;

        $paymentData["create_time"] = getCurrentDate();
        $paymentData["status"] = -1;
        $paymentData["customer_id"] = $this->customer["customer_id"];
        $paymentData["openid"] = $this->customer["openid"];
        $paymentData["name"] = I("post.name");
        $paymentData["amount"] = I("post.amount");
//        $paymentData["number"] = I("post.number");
//        $paymentData["sales_id"] = I("post.salesId");
        $paymentData["receive_contact"] = I("post.receiveContact");
        $paymentData["need_invoice"] = I("post.needInvoice");
        $paymentData["invoice_name"] = I("post.invoiceName");
        $paymentData["invoice_number"] = I("post.invoiceNumber");
        $paymentData["type"] = I("post.type");
        $paymentData["payment_content"] = I("post.paymentContent");

        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".access_token()."&openid=".$this->customer["openid"]."&lang=zh_CN";
        $info = json_decode(curl($url),true);
        $paymentData["headimgurl"] = $info["headimgurl"];
        $nicknameTmp = json_encode($info["nickname"]);
        $nicknameTmp = preg_replace_callback("#(\\\ud[0-9a-f]{3})#i",function($r){ return ""; },$nicknameTmp);
        $paymentData["wechat_name"] = json_decode($nicknameTmp);

        $payId = D('Pay')->add(array(
            'out_trade_no' => "PAID".date('Ymd',time()) . sprintf("%03d", substr(D("Pay")->count(), -3, 3)+1),
            'money' => $paymentData["amount"],
            'status' => 0,
            'type' => 0,
            'callback' => "",
            'create_time' => getCurrentDate(),
            'update_time' => getCurrentDate(),
            'pay_source' => 1
        ));

        $paymentData["pay_id"] = $payId;

        $paymentId = D("Payment")->add($paymentData);

        $data["payment_id"] = $paymentId;

        echo json_encode($data);

    }

    public function paymentList() {
        $id = $_REQUEST["id"];
        $status = $_REQUEST["status"];
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("user", $user);
        $this->assign("paymentId", $id);
        $this->assign("paymentStatus", $status);
        $this->display("paymentList");
    }

    public function myPaymentList() {
        $map["pm.status"] = array('neq','-3');
        $status = I("post.status");
        if(!isNullOrEmpty($status)) {
            $map["pm.status"] = $status;
        }
        if(!check_role($this->customer["customer_id"], "0,1,2,3,7,8,10")){
            $map["pm.customer_id"] = $this->customer["customer_id"];
        }
        $paymentId = I("post.paymentId");
        if(!isNullOrEmpty($paymentId) && I("post.ignorePaymentId") != '1') {
            $map["payment_id"] = $paymentId;
        }
        $employee = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
//        $map["pm.enterprise_code"] = $employee["enterprise_code"];
        $list = D("Payment")->alias("pm")
            ->join("left join __PAY__ pay on pay.pay_id = pm.pay_id")
            ->join("left join __CUSTOMER__ cus on cus.customer_id = pm.customer_id")
            ->where($map)
            ->order("pm.payment_id desc")
            ->field("pm.*,cus.nickname,cus.realname,pay.pay_trade_no")
            ->select();

        echo json_encode($list);
    }
}